package accel.activities;

import accel.dataholders.Character;
import accel.dataholders.Weapon;
import accel.storage.DBManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class ChooseWeaponActivity extends Activity{

	private Gallery portrait;
	private TextView name, str,ener,heal, cha, spe,desc, soulName, str2,ener2,heal2, cha2, spe2, str3,ener3,heal3, cha3, spe3, ls, ls2, ls3;
	private Weapon [] weapons;
	private int weaponId;
	private Character soul;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_weapon);
        weapons = Weapon.getWeapons();
        name = (TextView)findViewById(R.id.name);
        soul = Character.instance;
        
        str = (TextView)findViewById(R.id.strength);
        ener = (TextView)findViewById(R.id.energy);
        heal = (TextView)findViewById(R.id.health);
        cha = (TextView)findViewById(R.id.charisma);
        spe = (TextView)findViewById(R.id.speed);
        ls = (TextView)findViewById(R.id.lspeed);
        
        str2 = (TextView)findViewById(R.id.strength2);
        ener2 = (TextView)findViewById(R.id.energy2);
        heal2 = (TextView)findViewById(R.id.health2);
        cha2 = (TextView)findViewById(R.id.charisma2);
        spe2 = (TextView)findViewById(R.id.speed2);
        ls2 = (TextView)findViewById(R.id.lspeed2);
        
        str3 = (TextView)findViewById(R.id.strength3);
        ener3 = (TextView)findViewById(R.id.energy3);
        heal3 = (TextView)findViewById(R.id.health3);
        cha3 = (TextView)findViewById(R.id.charisma3);
        spe3 = (TextView)findViewById(R.id.speed3);
        ls3 = (TextView)findViewById(R.id.lspeed3);
       
        
        desc = (TextView)findViewById(R.id.description);
        soulName = (TextView)findViewById(R.id.soulName);
        portrait = (Gallery)findViewById(R.id.portrait);
        portrait.setAdapter(new ImageAdapter(this));
        portrait.setHorizontalFadingEdgeEnabled(true);
        portrait.setOnItemSelectedListener(new OnItemSelectedListener(){
        	public void onNothingSelected(AdapterView<?> a){}
        	
        	public void onItemSelected(AdapterView<?> adapter, View v, int i, long l)
        	{
        		name.setText(ChooseWeaponActivity.this.getString(weapons[i].getName()));
        		
        		str.setText(Integer.toString(soul.getStats().maxstrength));
        		ener.setText(Integer.toString(soul.getStats().maxenergy));
        		heal.setText(Integer.toString(soul.getStats().maxhealth));
        		cha.setText(Integer.toString(soul.getStats().maxcharisma));
        		spe.setText(Integer.toString(soul.getStats().maxspeed));
        		ls.setText(Integer.toString(soul.getStats().maxlearningspeed));
        		
        		setValues(soul.getStats().maxstrength, weapons[i].getStats().maxstrength, str2, str3);
        		setValues(soul.getStats().maxenergy, weapons[i].getStats().maxenergy, ener2, ener3);
        		setValues(soul.getStats().maxhealth, weapons[i].getStats().maxhealth, heal2, heal3);
        		setValues(soul.getStats().maxcharisma, weapons[i].getStats().maxcharisma, cha2, cha3);
        		setValues(soul.getStats().maxspeed, weapons[i].getStats().maxspeed, spe2, spe3);
        		setValues(soul.getStats().maxlearningspeed, weapons[i].getStats().maxlearningspeed, ls2, ls3);
        		
        		desc.setText(ChooseWeaponActivity.this.getString(weapons[i].getDesciption()));
        		weaponId = i;
        	}
        	private void setValues(int a, int b, TextView tx1, TextView tx2) //a - soul.stats, b - weapon.stats
        	{
        		if (b > 0)
        		{
        			tx1.setText("+" + Integer.toString(b));
        			tx1.setTextColor(Color.GREEN);
        			tx2.setText(Integer.toString(a + b));
        			//str3.setTextColor(Color.GREEN);
        			tx2.setBackgroundColor(Color.GREEN);
        		}
        		else if (b == 0)
        		{
        			tx1.setText("0");
        			tx1.setTextColor(Color.GRAY);
        			tx2.setText(Integer.toString(a));
        			//str3.setTextColor(Color.BLACK);
        			tx2.setBackgroundColor(Color.TRANSPARENT);
        		}
        		else
        		{
        			tx1.setText(Integer.toString(b));
        			tx1.setTextColor(Color.RED);
        			tx2.setText(Integer.toString(a + b));
        			//str3.setTextColor(Color.RED);
        			tx2.setBackgroundColor(Color.RED);
        		}
        	}
        });
        soulName.setText(" " + ChooseWeaponActivity.this.getString(soul.getName()));
	}
	
	public void nextScreen(View v)
	{
		DBManager db = DBManager.getInstance(this);
		Character.instance.setWeapon(weapons[weaponId]);
		db.saveNewCharacer(Character.instance);
		this.setResult(RESULT_OK);
		startActivity(new Intent(this, CharacterScreenActivity.class));
		finish();
	}
	
	public class ImageAdapter extends BaseAdapter {
	    int mGalleryItemBackground;
	    private Context mContext;

	    private Integer[] pics = Weapon.getDrawablesArray();

	    public ImageAdapter(Context c) {
	        mContext = c;
	    }

	    public int getCount() {
	        return pics.length;
	    }

	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView i = new ImageView(mContext);

	        i.setImageResource(pics[position]);

	        return i;
	    }
	}
}
